export const FuncTypeOptions = [
	{ value: '0', label: '菜单目录' },
	{ value: '1', label: '菜单' },
	{ value: '2', label: '按钮' },
];

export const FuncClassOptions = [
	{
		class: '0',
		label: '数据字典',
		treeType: '',
		isEdit: '0',
		cpnPath: 'linxyun/data_dict_tree',
		getFuncUrl: (urlBase, thisObj) => {
			if (urlBase.charAt(urlBase.length - 1) !== '/') {
				urlBase = urlBase + '/';
			}
			if (thisObj.isEditAble) {
				return urlBase + 'ty=' + thisObj.TreeType + '&ea=1';
			} else {
				return urlBase + 'ty=' + thisObj.TreeType + '&ea=0';
			}
		},
		parseUrl: (url, resultObj) => {
			const param = parseUrlParams(url);
			if (param === null) {
				return url;
			}

			resultObj.TreeType = param.ty || '';
			resultObj.isEditAble = false;
			if (param.ea && param.ea === '1') {
				resultObj.isEditAble = true;
			}
			const pathParamIdx = url.lastIndexOf('/');
			if (pathParamIdx < 0) {
				return url;
			}
			return url.substr(0, pathParamIdx);
		},
	},
	{
		class: '1',
		label: '自定义表单',
		pageID: '0',
		cpnPath: 'linxyun/customize_tab',
		getFuncUrl: (urlBase, thisObj) => {
			if (urlBase.charAt(urlBase.length - 1) !== '/') {
				urlBase = urlBase + '/';
			}
			return urlBase + 'PageID=' + thisObj.PageID;
		},
		parseUrl: (url, resultObj) => {
			const param = parseUrlParams(url);
			if (param === null) {
				return url;
			}
			resultObj.PageID = param.PageID || '';

			const pathParamIdx = url.lastIndexOf('/');
			if (pathParamIdx < 0) {
				return url;
			}
			return url.substr(0, pathParamIdx);
		},
	},
	{
		class: '2',
		label: '自定义页面',
		pageID: '0',
		cpnPath: 'linxyun/customize_page',
		getFuncUrl: (urlBase, thisObj) => {
			if (urlBase.charAt(urlBase.length - 1) !== '/') {
				urlBase = urlBase + '/';
			}
			return urlBase + 'PageID=' + thisObj.PageID;
		},
		parseUrl: (url, resultObj) => {
			const param = parseUrlParams(url);
			if (param === null) {
				return url;
			}
			resultObj.PageID = param.PageID || '';

			const pathParamIdx = url.lastIndexOf('/');
			if (pathParamIdx < 0) {
				return url;
			}
			return url.substr(0, pathParamIdx);
		},
	},
	{
		class: '3',
		label: '账号管理',
		cpnPath: 'linxyun/usermanage/accountManager',
		getFuncUrl: (urlBase, thisObj) => {
			return urlBase;
		},
		parseUrl: (url, resultObj) => {
			return url;
		},
	},
	{
		class: '4',
		label: '角色管理',
		cpnPath: 'linxyun/usermanage/roleManage',
		getFuncUrl: (urlBase, thisObj) => {
			return urlBase;
		},
		parseUrl: (url, resultObj) => {
			return url;
		},
	},
	{
		class: '5',
		label: '快速报表',
		pageID: '0',
		cpnPath: 'linxyun/customize_report',
		getFuncUrl: (urlBase, thisObj) => {
			if (urlBase.charAt(urlBase.length - 1) !== '/') {
				urlBase = urlBase + '/';
			}
			return urlBase + 'PageID=' + thisObj.PageID;
		},
		parseUrl: (url, resultObj) => {
			const param = parseUrlParams(url);
			if (param === null) {
				return url;
			}
			resultObj.PageID = param.PageID || '';

			const pathParamIdx = url.lastIndexOf('/');
			if (pathParamIdx < 0) {
				return url;
			}
			return url.substr(0, pathParamIdx);
		},
	},
	{
		class: '99',
		label: '其他',
		parseUrl: (url, resultObj) => {
			return url;
		},
	},
];

export function getFuncClassInfo(funcClass) {
	for (let i = 0; i < FuncClassOptions.length; i++) {
		if (funcClass === FuncClassOptions[i].class) {
			return FuncClassOptions[i];
		}
	}
	return null;
}

function parseUrlParams(url) {
	const pathParamIdx = url.lastIndexOf('/');
	if (pathParamIdx < 0) {
		return null;
	}

	const paramString = url.slice(pathParamIdx + 1);
	if (paramString === undefined || paramString === '') {
		return null;
	}
	const params = paramString.split('&');

	const parmaList = {};
	let keyValue = null;
	for (let i = 0; i < params.length; i++) {
		keyValue = params[i].split('=');
		if (!keyValue || keyValue.length !== 2) {
			continue;
		}
		parmaList[keyValue[0]] = keyValue[1];
	}

	return parmaList;
}
